package com.lovo.project.servlet.product;

import com.lovo.project.enums.ProductEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.service.ProductService;
import com.lovo.project.service.impl.ProductServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @title 商品修改url
 * @author: chenqiang
 * @date: 2020/11/4 14:09
 */
@WebServlet(name = "ProductUpdateServlet",value = "/product/productUpdate")
public class ProductUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
             request.setCharacterEncoding("utf-8");
             response.setCharacterEncoding("utf-8");
             response.setContentType("text/html; charset=UTF-8");


             ProductService productService =new ProductServiceImpl();
             Integer productId =Integer.parseInt(request.getParameter("productId"));
             System.out.println(productId );
             Integer productMoney =Integer.parseInt(request.getParameter("productMoney")) ;
             String productExclusiveSpecification =request.getParameter("productExclusiveSpecification");
             PrintWriter out = response.getWriter();

             try {
            productService.updateProduct(productId,productMoney,productExclusiveSpecification);
            out.print(ResultVO.success().toJson());
             } catch (OurException e) {
            out.print(ResultVO.fail(e.getCodeEnum()).toJson());
            }catch (Exception e1){
            out.print(ResultVO.fail(ProductEnum.PRODUCT_Update_ENUM).toJson());
            }finally {
            out.close();
           }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     doPost(request,response);
    }
}
